mui.init();

var old_back = mui.back;
mui.back = function(){
	var targetPageId = plus.storage.getItem(KeyCfg.targetPageId);	
	if(!targetPageId){
		mui.alert("参数：targetPageId 为 null ,无法跳转到下一步");
		return;
	}
	var targetPage = plus.webview.getWebviewById(targetPageId);
    mui.fire(targetPage,'reload',{});
    targetPage.show();
}

mui.plusReady(function(){

	var name = plus.storage.getItem(KeyCfg.userName);
	$("#name").val(name);
	
	
	var phone_card = CmwMcore.$('phone_card');
	MeventMgr.onTouchstart(phone_card,function(event,self){
		var a= document.getElementById("a");
		a.checked=true;
		$(".qypwd").show();
		$(".emailpwd").hide();
		$(".resetpwd").hide();
	});
	
	var email_card = CmwMcore.$('email_card');
	MeventMgr.onTouchstart(email_card,function(event,self){
		var b= document.getElementById("b");
		b.checked=true;
		$(".qypwd").hide();
		$(".emailpwd").show();
		$(".resetpwd").hide();
	});
	
	/**------- 发送新号码验证码  -------*/
	var sendNewPhone = CmwMcore.$('sendNewPhone');
	MeventMgr.onTouchstart(sendNewPhone,function(event,self){
		var dis=$("#sendNewPhone").attr("disabled")//判断是否有这属性
		if(!valid()) return;
		$("#sendNewPhone").html("短信发送中...");
		$("#sendNewPhone").css('opacity',0.5);
		$("#sendNewPhone").prop("disabled", true); 
		var url = UrlConfig.muser_checkmobile;
		var pars = {phone:$("#phoneNum").val()};
		MuiEventMgr.ajax(url,{pars:pars,
		sfn:function(data){
			if(data.success){
				sendNewPhoneCode();
			}else{
				mui.alert(data.msg);
				$("#sendNewPhone").html("发送验证码");
				$("#sendNewPhone").css('opacity',1);
				$("#sendNewPhone").prop("disabled", false); 
			}
		},
		ffn:function(err){
			mui.alert("获取数据失败：["+err+"]");
		}});
	});
	
	/**------- 邮箱验证码+身份证  -------*/
	var btn_emailpwd = CmwMcore.$('btn_emailpwd');
	MeventMgr.onTouchstart(btn_emailpwd,function(event,self){
		if(!valid1()) return;
		submitForm1();
	});
	
	/**------- 手机验证码+身份证  -------*/
	var btn_phone = CmwMcore.$('btn_phone');
	MeventMgr.onTouchstart(btn_phone,function(event,self){
		if(!valid2()) return;
		submitForm2();
	});
	
	/**------- 提交新手机号码  -------*/
	var btn_submit = CmwMcore.$('btn_submit');
	MeventMgr.onTouchstart(btn_submit,function(event,self){
		if(!valid3()) return;
		submitForm3();
	});
	
	
	function sendNewPhoneCode(){
		var phonenum = $("#phoneNum").val();
		var smstype ="validPassword2";
		var isNew = true;	
		var guid = plus.storage.getItem(KeyCfg.userId);
		var pars={userId:guid,phonenum:phonenum,smstype:smstype,isNew:isNew};
		var url = UrlConfig.msms_index;
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		callback(data);	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败：["+err+"]");
		  	}
		});
	}
	
	function callback(data){
		var msg = data.msg;
		if (msg == 1) {
			mui.alert("短信发送成功");
			fun_timedownn(120);
			//$('#sendNewPhone').attr("disabled", true);
		} else if (msg == 0) {
			$("#sendNewPhone").html("重新发送");
			$('#sendNewPhone').attr("disabled", false);
			mui.alert("短信发送失败");
			
		} else if (msg == 2) {
			$("#sendNewPhone").html("重新发送");
			$('#sendNewPhone').attr("disabled", false);
			mui.alert("发送频率过高，2分钟之内不能重复发送");
			
		} else if (msg == 3) {
			$("#sendNewPhone").html("重新发送");
			$('#sendNewPhone').attr("disabled", false);
			mui.alert("没有要发送的号码");
			
		} else if (msg == 4) {
			$("#sendNewPhone").html("重新发送");
			$('#sendNewPhone').attr("disabled", false);
			mui.alert("您还没有登录呢，不能发送短信");
			
		}
	}
	
	function submitForm1(){
		var card = $("#card1").val();
		var emailcode =$("#emailcode").val();
		var url =UrlConfig.checkEmailCard;
		var pars={card:card,emailcode:emailcode};
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if (status == "true") {
                	mui.alert("验证成功");
                	$(".qbox").hide();
                    $(".qypwd").hide();
					$(".emailpwd").hide();
					$(".resetpwd").show();
                } else if (status == "errorCard") {
                	mui.alert("身份证号码错误");                	
                } else if (status == "errorEmail") {
                	mui.alert("邮箱验证失败");            	
                } else {
                	mui.alert("验证失败");               	
                }
	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败");
		  	}
		});
	}
	
	
	
	function valid1(){
		var cardid_val = $("#card1").val();
		var emailcode_val =$("#emailcode").val();
		
		if(!cardid_val || !cardid_val.trim()){
			mui.alert("请输入您的身份证号!");
			return false;
		}
		
		if(!emailcode_val || !emailcode_val.trim()){
			mui.alert("请输入验证码!");
			return false;
		}
		
		var statuss = false;
		card = cardid_val.trim();
		if (card.length == 15 || card.length == 18) {
			//中国身份证 15位或者18位
			statuss = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(card);
		} else if (/(^[A-Z]$)/.test(card.substr(0, 1))) {
			//香港身份证以字母开头，只验证是否以字母开头，不验证长度
			statuss = true;
		} else if (card.length == 8) {
			//港澳身份证
			statuss = /(^[A-Z0-9]\d{6}[A-Z0-9]$)/.test(card);
		}
		if(!statuss){
			mui.alert("身份证号码格式错误!");
			return false; 
		}
		return true;
	}
	
	function valid2(){
		var cardid_val = $("#card2").val();
		var phonecode_val =$("#phonecode").val();
		
		if(!cardid_val || !cardid_val.trim()){
			mui.alert("请输入您的身份证号!");
			return false;
		}
		
		if(!phonecode_val || !phonecode_val.trim()){
			mui.alert("请输入验证码!");
			return false;
		}
		
		var statuss = false;
		card = cardid_val.trim();
		if (card.length == 15 || card.length == 18) {
			//中国身份证 15位或者18位
			statuss = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(card);
		} else if (/(^[A-Z]$)/.test(card.substr(0, 1))) {
			//香港身份证以字母开头，只验证是否以字母开头，不验证长度
			statuss = true;
		} else if (card.length == 8) {
			//港澳身份证
			statuss = /(^[A-Z0-9]\d{6}[A-Z0-9]$)/.test(card);
		}
		if(!statuss){
			mui.alert("身份证号码格式错误!");
			return false; 
		}
		return true;
	}
	
	function valid(){
		var num = $("#phoneNum").val();			
		if(!num || !num.trim()){
			mui.alert("请输入新手机号码!");
			return false;
		}		
				
		var status = /^1[0-9][0-9]\d{8}$/.test(num);
		if(!status){
			mui.alert("手机号码格式错误");
			return false;
		}
		return true;
	}
	
	function valid3(){
		var num = $("#phoneNum").val();
		var phone = $("#phone").val();
		
		if(!num || !num.trim()){
			mui.alert("请输入新手机号码!");
			return false;
		}
		
		if(!phone || !phone.trim()){
			mui.alert("请输入验证码!");
			return false;
		}
		
		var status = /^1[0-9][0-9]\d{8}$/.test(num);
		if(!status){
			mui.alert("手机号码格式错误");
			return false;
		}
		return true;
	}
	
//      var yzm = $("#phone").focus();
//      alert(yzm);
//		if(yzm!=null){
//		    $('#phoneNum').attr("disabled", true);
//			//mui.alert("新手机号码与收到验证码的手机号码不一致!");
//			//return false;
//		}

$("#phoneNum").focus(function(){
			var phone = $("#phone").val();
		    if(phone!=null&&phone!=""){
		    $('#phoneNum').attr("disabled", true);
			}
		});
		
		$("#phone").focus(function(){
			var phone = $("#phone").val();
		    if(phone!=null&&phone!=""){
		    $('#phoneNum').attr("disabled", true);
			}
		});
		
		
//		$("#phone").blur(function(){
//			
//			var phone = $("#phone").val();
//		    if(phone==null||phone==""){
//		    $('#phoneNum').attr("disabled", false);
//			}
//		});
	

	
	function submitForm3(){
		var phoneNum = $("#phoneNum").val();

		var phone = $("#phone").val();

		var smstype = "validPassword2";
		
		var pars ={phoneNum:phoneNum,phone:phone,smstype:smstype}
		var url =UrlConfig.updatePhone;
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if (status == "true") {
		  			mui.alert("修改手机号码成功");
					mui.back();				
				} else if (status == "error") {
					mui.alert("手机验证码错误");					
				} else if (status == "overTime") {
					mui.alert("手机验证码失效");						
				} else {
					mui.alert("验证失败");					
				}
	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败");
		  	}
		});
		
	}
	
	 //手机验证+身份证
    function submitForm2() {
        var card = $("#card2").val();
		var code =$("#phonecode").val();
		var smstype ="validPassword";
		var url =UrlConfig.checkPhoneCard;
		var pars={card:card,phone:code,smstype:smstype};
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if (status == "true") {
	            	mui.alert("验证成功");
	            	$(".qbox").hide();
	            	$(".qypwd").hide();
					$(".emailpwd").hide();
					$(".resetpwd").show();
	            } else if (status == "errorCard") {
	            	mui.alert("身份证号码错误");                	
	            } else if (status == "error") {
	            	mui.alert("手机验证错误"); 
	            } else if (status == "overTime") {
	            	mui.alert("手机验证码失效");        
	            } else {
	            	mui.alert("验证失败");               	
	            }
	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败");
		  	}
		});
    }
    
    
});

function fun_timedownn(time)  {
    if(!time && time !== 0) time = 120;	        	
	$("#sendNewPhone").html(time+"秒");	    
    time = time-1;
    if(time>=0){
        setTimeout("fun_timedownn("+time+")",1000);
    }else {
    	$("#sendNewPhone").html("重新获取");
    	$("#sendNewPhone").css('opacity',1);
    	$("#sendNewPhone").prop("disabled", false);
    }
}

